#!/bin/bash

LOG_DIRECTORY="./Reports"

regexLtOrLe="Current version: '(.*)' \(less than '(.*)' or equal to '(.*)'\). "             # Regular expression to extract versions
regexEq="Current version: '(.*)' is equal to '(.*)'\. "                                     # Regular expression to extract versions
regexDefault="Match found, the package '.*' is vulnerable to '.*' due to default status\. " # Regular expression to extract default status

# Function: compare_and_validate_rpm
# Description: Compares two RPM versions extracted from a given line and validates the comparison result.
# Parameters:
#   $1 - A string representing a line from a log file containing version information.
# Return:
#   Returns 0 if the comparison result indicates that the first version is less than the second version or equal to the third version. Returns 1 otherwise.
# Example Usage:
#   compare_and_validate_rpm "Current version: '1.0' (less than '1.1' or equal to '1.0')."
compare_and_validate_rpm() {
    local line="$1" # The line containing version information
    local result="" # Variable to store the comparison result

    if [[ $line =~ $regexDefault ]]; then
        return 0                                            # Return success
    elif [[ "$line" =~ $regexLtOrLe ]]; then                # Check if the line matches the regular expression
        version1="${BASH_REMATCH[1]}"                       # Extract the first version
        version2="${BASH_REMATCH[2]}"                       # Extract the second version
        version3="${BASH_REMATCH[3]}"                       # Extract the third version
        if [ -n "$version2" ]; then                         # Check if the second version exists
            result=$(rpmdev-vercmp "$version1" "$version2") # Compare first and second versions
        else
            result=$(rpmdev-vercmp "$version1" "$version3") # Compare first and third versions
        fi
        # Check if the comparison result indicates that the first version is less than the second version or equal to the third version
        if [[ "$result" =~ "<" || "$result" =~ "=" ]]; then
            return 0 # Return success
        else
            return 1 # Return failure
        fi
    elif [[ "$line" =~ $regexEq ]]; then                # Check if the line matches the regular expression
        version1="${BASH_REMATCH[1]}"                   # Extract the first version
        version2="${BASH_REMATCH[2]}"                   # Extract the second version
        result=$(rpmdev-vercmp "$version1" "$version2") # Compare the two versions
        # Check if the comparison result indicates that the two versions are equal
        if [[ "$result" =~ "==" ]]; then
            return 0 # Return success
        fi
    fi
    return 1 # Return failure in case the regular expression doesn't match
}

# Function: compare_and_validate_dpkg
# Description: Compares two DPKG versions extracted from a given line and validates the comparison result.
# Parameters:
#   $1 - A string representing a line from a log file containing version information.
# Return:
#   Returns 0 if the comparison result indicates that the first version is less than the second version or equal to the third version. Returns 1 otherwise.
# Example Usage:
#   compare_and_validate_dpkg "Current version: '1.0' (less than '1.1' or equal to '1.0')."
compare_and_validate_dpkg() {
    local line="$1" # The line containing version information
    local result="" # Variable to store the comparison result

    if [[ $line =~ $regexDefault ]]; then
        return 0 # Return success
    elif [[ "$line" =~ $regexLtOrLe ]]; then
        # Check if the line matches the regular expression
        version1="${BASH_REMATCH[1]}"                                              # Extract the first version
        version2="${BASH_REMATCH[2]}"                                              # Extract the second version
        version3="${BASH_REMATCH[3]}"                                              # Extract the third version
        if [ -n "$version2" ]; then                                                # Check if the second version exists
            result=$(dpkg --compare-versions "$version1" lt "$version2" && echo 1) # Compare first and second versions
        else
            result=$(dpkg --compare-versions "$version1" le "$version3" && echo 1) # Compare first and third versions
        fi
        # Check if the comparison result indicates that the first version is less than the second version or equal to the third version
        if [[ "$result" != 0 ]]; then
            return 0 # Return success
        else
            return 1 # Return failure
        fi
    elif [[ "$line" =~ $regexEq ]]; then
        # Check if the line matches the regular expression
        version1="${BASH_REMATCH[1]}"                                          # Extract the first version
        version2="${BASH_REMATCH[2]}"                                          # Extract the second version
        result=$(dpkg --compare-versions "$version1" eq "$version2" && echo 1) # Compare the two versions
        # Check if the comparison result indicates that the two versions are equal
        if [[ "$result" != 0 ]]; then
            return 0 # Return success
        fi
    fi

    return 1 # Return failure in case the regular expression doesn't match
}

# Main script
main() {
    echo "=== Version Verification ==="
    # Check if the log directory exists
    if [ ! -d "$LOG_DIRECTORY" ]; then
        echo "Directory not found: $LOG_DIRECTORY"
        exit 1
    fi

    # Define the callback function based on the Linux distribution
    local callback=''
    if [ -r "/etc/centos-release" ]; then
        callback=compare_and_validate_rpm
    else
        callback=compare_and_validate_dpkg
    fi

    # Iterate over the log files in the directory
    for file in "$LOG_DIRECTORY"/*; do
        if [ -f "$file" ]; then
            while IFS= read -r line; do
                # Check if the comparison is correct
                if ! $callback "$line"; then
                    echo "=== ERROR === Version verification failed for registry: $line"
                fi
            done <"$file"
        fi
    done
}

main # Call the main function.
echo "=== Version Verification Completed ==="
